package com.tanhua.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tanhua.dubbo.api.db.SettingsApi;
import com.tanhua.dubbo.api.mapper.SettingsMapper;
import com.tanhua.model.db.Question;
import com.tanhua.model.db.Settings;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

@DubboService
public class SettingsApiImpl implements SettingsApi {

    @Autowired
    private SettingsMapper settingsMapper;

    /**
     * 通过用户id查询通知设置
     *
     * @param userId
     * @return
     */
    @Override
    public Settings findByUserId(Long userId) {
        LambdaQueryWrapper<Settings> lqw = new LambdaQueryWrapper();
        lqw.eq(Settings::getUserId, userId);
        return settingsMapper.selectOne(lqw);
    }

    /**
     * 保存通知设置
     *
     * @param pojo
     */
    @Override
    public void save(Settings pojo) {
        //1. 通过用户id查询通知设置
        Settings settingsInDB = findByUserId(pojo.getUserId());
        if(null != settingsInDB) {
            //2. 存在则更新
            pojo.setId(settingsInDB.getId());
            settingsMapper.updateById(pojo);
        }else {
            //3. 不存在则添加
            settingsMapper.insert(pojo);
        }
    }
}
